package com.sftz.modules.minapp.courseType.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.sftz.assets.entities.CourseTypeEntity;
import com.sftz.assets.enums.CourseStateEnum;
import com.sftz.modules.minapp.courseType.form.QueryCourseDto;
import com.sftz.modules.minapp.courseType.service.CourseTypeService;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

@IocBean(name = "courseTypeService")
public class CourseTypeServiceImpl implements CourseTypeService {

	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	//进首页默认加载
	@Override
	public Object courseTypeData(Integer id, Integer searchPageNum, Integer callbackcount) {
		//id是从首页传过来的一级分类
		Map<String, Object> obj = new HashMap<String, Object>();
		//课程一级分类
		List<CourseTypeEntity> objone = dbDao.query(CourseTypeEntity.class, Cnd.where("pid", "=", 0), null);
		obj.put("objone", objone);
		//查询二级分类下第一个分类下的课程
		if (Util.isEmpty(id)) {
			//查询一级分类第一个分类下的二级分类
			CourseTypeEntity courseTypeEntity = objone.get(0);
			List<CourseTypeEntity> queryTypeTwo = queryTypeTwo(courseTypeEntity.getId());
			obj.put("objtwo", queryTypeTwo);
			//默认展示该一级分类下的所有课程
			List<QueryCourseDto> oneCourse = queryOneCourseList(courseTypeEntity.getId(), searchPageNum, callbackcount);
			obj.put("courseshow", oneCourse);
		} else {
			List<CourseTypeEntity> queryTypeTwo = queryTypeTwo(id);
			obj.put("objtwo", queryTypeTwo);
			List<QueryCourseDto> oneCourse = queryOneCourseList(id, searchPageNum, callbackcount);
			obj.put("courseshow", oneCourse);
		}
		return obj;

	}

	//点击一级分类查询一级分类下所有课程
	@Override
	public Object queryType(Integer id, Integer searchPageNum, Integer callbackcount) {
		Map<String, Object> obj = new HashMap<String, Object>();
		//课程二级分类
		List<CourseTypeEntity> objtwo = dbDao.query(CourseTypeEntity.class, Cnd.where("pid", "=", id), null);
		obj.put("objtwo", objtwo);
		List<QueryCourseDto> oneCourse = queryOneCourseList(id, searchPageNum, callbackcount);
		obj.put("courseshow", oneCourse);
		return obj;

	}

	//根据二级分类查询课程
	@Override
	public Object queryCourse(Integer id, Integer searchPageNum, Integer callbackcount) {
		Map<String, Object> obj = new HashMap<String, Object>();
		List<QueryCourseDto> queryCourseList = queryCourseList(id, searchPageNum, callbackcount);
		obj.put("courseshow", queryCourseList);
		return obj;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年12月11日  
	 * (effect:根据一级分类查询二级分类)
	 */
	public List<CourseTypeEntity> queryTypeTwo(Integer id) {
		List<CourseTypeEntity> objtwo = dbDao.query(CourseTypeEntity.class, Cnd.where("pid", "=", id), null);
		return objtwo;
	}

	/**
	 * @author   DuKL
	 * @Date	 2018年12月11日
	 * (effect:根据二级分类查询下面的课程)
	 */
	public List<QueryCourseDto> queryCourseList(Integer id, Integer searchPageNum, Integer callbackcount) {
		int start = (searchPageNum - 1) * callbackcount;
		Sql courseAllshow = Sqls.create(sqlManager.get("minapp_courseType_course_list"));
		Cnd cnd = Cnd.NEW();
		cnd.and("c.courseState", "=", CourseStateEnum.PUBLISHED.intKey()).and("a.id", "=", id);
		courseAllshow.setCondition(cnd);
		courseAllshow.setParam("start", start);
		courseAllshow.setParam("end", callbackcount);
		List<QueryCourseDto> course = DbSqlUtil.query(dbDao, QueryCourseDto.class, courseAllshow);
		return course;
	}

	/**
	 * 展示该一级分类下的所有课程
	 */
	public List<QueryCourseDto> queryOneCourseList(Integer id, Integer searchPageNum, Integer callbackcount) {
		int start = (searchPageNum - 1) * callbackcount;
		Sql courseAllshow = Sqls.create(sqlManager.get("minapp_courseType_course_typeone_list"));
		courseAllshow.setParam("courseType", id);
		courseAllshow.setParam("start", start);
		courseAllshow.setParam("end", callbackcount);
		List<QueryCourseDto> course = DbSqlUtil.query(dbDao, QueryCourseDto.class, courseAllshow);
		return course;
	}

	@Override
	public Object queryOneCourse(Integer oneId, Integer searchPageNum, Integer callbackcount) {
		Map<String, Object> obj = new HashMap<String, Object>();
		List<QueryCourseDto> oneCourse = queryOneCourseList(oneId, searchPageNum, callbackcount);
		obj.put("courseshow", oneCourse);
		return obj;
	}

}
